using Imaginarium.battleSistem;
using Imaginarium.team;
using System.Collections.Generic;
using System;

namespace Imaginarium.IA
{
    public class IATeam : TeamController
    {
        #region Attributes
        public Team thisTeam { get; set; }
        private List<TeamAggro> enemiesAggroes;
        #endregion

        #region Constructors
        public IATeam(Team team, List<Team> enemies):
            base()
        {
            enemiesAggroes = new List<TeamAggro>(enemies.Count);
            this.thisTeam = team;
            foreach (Team tEnemy in enemies)
            {
                enemiesAggroes.Add(new TeamAggro(tEnemy));
            }
        }
        public IATeam(Team team):
            base()
        { 
            this.thisTeam = team;
            this.enemiesAggroes = null;
        }
        #endregion

        #region Functionality
        public List<BattleCommand> generateTeamCommands()
        {
            TurnExpert turnExpert = BattleManager.getInstance().turnExpert;
            if (enemiesAggroes == null) //we fill them with the other teams from turnExpert
            {
                enemiesAggroes = new List<TeamAggro>();
                List<Team> enemyTeams = new List<Team>(turnExpert.m_Teams.Count);
                enemyTeams.AddRange(turnExpert.m_Teams);
                enemyTeams.Remove(thisTeam); //myself is not an enemy
                foreach (Team tEnemy in enemyTeams)
                {
                    enemiesAggroes.Add(new TeamAggro(tEnemy));
                }
            }

            int teamCharAmount = thisTeam.characters.Count;
            TeamAggro mostHatedTeam = TeamAggro.getHighestAggroTeam(enemiesAggroes);
            Character mostHatedCharacter = mostHatedTeam.getHighestCharacterAggro();

            Random rand = new Random();
            List<BattleCommand> commands = new List<BattleCommand>(teamCharAmount);
            BattleCommand command = null;

            for (int i = 0; i < teamCharAmount; i++) 
            {
                Character character = thisTeam.characters[i];
                if (character.isDead())
                    continue;
                if (rand.Next(0, 10) > 6) //in 1/3 of the cases
                {
                    SkillDetail skillToUse;
                    List<SkillDetail> possibleSkills;
                    if (character is Player)
                    {
                        Player player = (Player)character;
                        possibleSkills = player.currentJob.skillDetail;
                    }
                    else //if character is enemy
                    {
                        Enemy enemy = (Enemy)character;
                        possibleSkills = enemy.skillDetails;
                    }
                    if (possibleSkills.Count < 1) //no skills available, so lets attack
                    {
                        command = new AttackCommand(character, mostHatedCharacter);
                    }
                    else
                    {
                        skillToUse = possibleSkills[rand.Next(0, possibleSkills.Count)];
                        command = new AbilityCommand(character, mostHatedCharacter, skillToUse);
                    }
                }
                else // in the other 2/3 of the cases just attack
                {
                    command = new AttackCommand(character, mostHatedCharacter);
                }
                commands.Add(command);
                BattleManager.getInstance().addToCommandStack(command);
            }

            //Cambiar onTeamFinishedTakenAction por uno que sea por primera vez
            turnExpert.onTeamFinishedTakenAction();
            return commands;
        }
        #endregion

    }
}